<?php

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use GenerateCrudService;
use TestingConfig;
use TestingAlgoHelper;
use TestingDetailService;
use TestingItemService;
use DeveloperLinkService;
use CategoryService;
use TestingDeveloperService;
use App\Services\AESHelper;

use App\Services\DeveloperHelper;

use App\Services\IdEncryptService;

use App\Services\Result\DecreaseWeightResult;
use App\Services\Algo\DecreaseWeightAlgo;

use App\Models\LogHelper;
use App\Models\TestingDeveloper;
use App\Services\Result\RecipeTestingResult;
use DeveloperTestingRelService;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\Redis;

class ExampleController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(YourModel::class, function (Grid $grid) {

            $grid->id('ID')->sortable();

            $grid->created_at();
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(YourModel::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }

    public function getPath(){
        $data = GenerateCrudService::generate();
        echo $data;
    }


    public function test(){
//        $options = TestingConfig::buildSelectOptions('middle_var');
//        dd($options);

//        $tid = 2;
//        $answerStr = '1:1:名字##2:4:性别##3:5:历史';
//        $score = TestingAlgoHelper::calculate($tid, $answerStr);
//        echo $score;
//        $str = '[{"key":"\u662f","weight":"3"},{"key":"\u5426","weight":"10"}]';
//        $item = TestingItemService::getById(5);
//        $options = $item['options'];
//        $d = json_decode($options,true);
//        dd($d);

//        $code = DeveloperHelper::generateDevUnionCode();
//        echo $code;
//        $key = 'qiezi';
//        $url = 'http://www.baidu.com/#home';
//        echo base64_encode($url);
//        LogHelper::log('aaaaa','user');
//        echo 1;

        $finalAnswerStr = '';
//        $obj = new DecreaseWeightResult();
//        $result = $obj->result();
//        echo $result;

//        $id = 23;
//        $detail = TestingDetailService::getById($id);
//        $detail['item'] = $detail->items();
//        return apiSuccess($detail);


//        $detail = TestingDeveloper::where('union_code','59FF7A-2AAEB4-2600C9-87DE90');
//        return apiSuccess($detail);
//        $list = TestingDeveloperService::getByUnionCode('59FF7A-2AAEB4-2600C9-87DE90');
//        return apiSuccess($list);
//        $list  = DeveloperLinkService::getPositionOption();
//        return apiSuccess($list);
//        $list  = DeveloperLinkService::getLinksByUnionCode('59FF7A-2AAEB4-2600C9-87DE90');
//        return apiSuccess($list);
//        $list  = DeveloperLinkService::getClinetLink('59FF7A-2AAEB4-2600C9-87DE90');
//        return apiSuccess($list);

//        $appid = 'qz36285b10d5420b6c';
//        $appsecret = 'ffd6bc4eda9cae6d6dc55a38444cc6a63cea5';
//        $timestamp = time();
//        $params = [];
//        array_push($params, $appid);
//        array_push($params, $appsecret);
//        array_push($params, $timestamp);
//        sort($params);
//            $signStr = implode("", $params);
//        $mdStr = md5($signStr);
//        $data['appid'] = $appid;
//        $data['appsecret'] = $appsecret;
//        $data['timestamp'] = $timestamp;
//        $data['mdStr'] = $mdStr;
//        $data['signStr'] = $signStr;
//        return apiSuccess($data);

//        $list  = CategoryService::getCateOptions();
//        return apiSuccess($list);

//        $developerOption = TestingDeveloper::all()->pluck('name','union_code');
//        return apiSuccess($developerOption);

//        $dwAnswerStr = '1:0:165##2:0:53##3:1:男##4:0:23##5:1:腰腹部最为肥胖##6:1:A型##7:1:腰腹部最为肥胖##8:1:腰腹部最为肥胖##9:2:一天两顿以上外卖或馆子##10:0:牛奶##11:-1:且不喜欢或没时间运动##12:-1:轻体力，几乎坐着##13:-1:不到11点不睡觉##14:1:减少肉类摄入##15:3:3##16:1:备孕期##17:1:有##18:1:习惯性便秘##19:16:柯兴综合症##20:0:crelaber';
//        $tid = 21;
//        $typeFourExpress = TestingItemService::typeFourItem($tid);
//        $instance = new DecreaseWeightAlgo($tid,$dwAnswerStr,$typeFourExpress);
//        $allAnswer = $instance->getAllAnswer();
//        echo json_encode($allAnswer);
//        echo $instance->resultToStr();

        //提交算法测试接口
//        $data['tid'] = 24;
//        $data['answers'] = '1:0:女##2:100:10-12个月##3:0:165##4:0:55##5:90:怀孕中期##6:42:电离辐射##7:22.8:手术后##8:0:无上述情况##9:16.21:高血脂##10:9.41:胃病##11:0:无上述情况##12:0:无上述情况##13:0:无上述情况##14:19.6:喘咳##15:0:无上述情况##16:0:无上述情况##17:7:便溏 粘马桶##18:5:夜尿多##19:7.14:胸肌大些##20:7.07:秀发飘逸##21:5.05:肾虚 你懂的。。。##22:7.14:天寒地冻 出门冻成狗';
//        $typeFourExpress = TestingItemService::typeFourItem($data['tid']);
//        $typeFourExpress = empty($typeFourExpress) ? null : $typeFourExpress;
//        $reslut = TestingAlgoHelper::calculate($data['tid'],$data['answers'],$typeFourExpress);
//        return apiSuccess($reslut);

//        //提交结论测试接口
//        $finalAnswerStr = '1:0:女##2:100:10-12个月##3:0:165##4:0:55##5:50:备孕##6:42:化学污染##7:0:无上述情况##8:0:无上述情况##9:16.3:动脉硬化##10:19.21:胰腺炎|||胃炎##11:9.4:乳腺炎##12:0:无上述情况##13:0:无上述情况##14:0:无上述情况##15:0:无上述情况##16:70:舌体胖大 苔白腻或舌红苔黄腻##17:7:腹泻##18:5:夜尿多##19:7.14:胸肌大些##20:7.07:秀发飘逸##21:5.05:肾虚 你懂的。。。##22:14.49:听说最近流感挺多的 宝宝有点怕怕|||天寒地冻 出门冻成狗##23:20.20202020202:0';
//
//        //一级标签为0，二级标签个数为0,bmi正常，三级标签为0,4级标签>=4,结果正常
//        $finalAnswerStr = '1:1:男##2:0:22-40岁##3:0:165##4:0:60##5:0:无上述情况##6:0:无上述选项##7:0:无上述情况##8:0:无上述情况##9:0:无上述情况##10:0:无上述情况##11:0:无上述情况##12:0:无上述情况##13:0:无上述情况##14:0:无上述情况##15:0:无上述情况##16:0:无上述情况##17:7:腹泻##18:5:夜尿多##19:7.14:胸肌大些##20:7.07:秀发飘逸##21:5.05:肾虚 你懂的。。。##22:3:无上述情况##23:22.038567493113:0';
//
//        //一级标签为2，二级标签个数为0,bmi正常，三级标签为0 ，4级标签>=4,结果正常
//        $finalAnswerStr = '1:0:女##2:100:10-12个月##3:0:165##4:0:60##5:90:怀孕晚期##6:0:无上述选项##7:0:无上述情况##8:0:无上述情况##9:0:无上述情况##10:0:无上述情况##11:0:无上述情况##12:0:无上述情况##13:0:无上述情况##14:0:无上述情况##15:0:无上述情况##16:0:无上述情况##17:7:便溏 粘马桶##18:10:易失眠、多梦|||自汗盗汗##19:7.07:上围C+cup##20:7.07:秀发飘逸##21:5.05:肾虚 你懂的。。。##22:3:无上述情况##23:22.038567493113:0';
//        //一级标签为0，二级标签个数为2,bmi正常，bmi=17.959183673469，三级标签为3 ，四级为0,结果不正常，bmi没有做判断
//        $finalAnswerStr = '1:1:男##2:0:22-40岁##3:0:175##4:0:55##5:0:无上述情况##6:42:电离辐射##7:15.6:糖尿病##8:0:无上述情况##9:32.81:心脏病|||高血压##10:0:无上述情况##11:0:无上述情况##12:0:无上述情况##13:0:无上述情况##14:0:无上述情况##15:0:无上述情况##16:0:红润##17:0:次数规律 色泽正常##18:0:良好##19:0:我就是男/女神##20:0:颜值担当##21:0:工作轻松 没啥操心的##22:3:无上述情况##23:17.959183673469:0';
        //tag1
//        $finalAnswerStr = '1:0:女##2:50:7-22岁##3:0:180##4:0:50##5:0:无上述情况##6:42:电离辐射##7:0:无上述情况##8:0:无上述情况##9:0:无上述情况##10:0:无上述情况##11:0:无上述情况##12:0:无上述情况##13:0:无上述情况##14:0:无上述情况##15:0:无上述情况##16:70:舌体黯紫有瘀点##17:0:次数规律 色泽正常##18:0:无上述情况##19:0:我就是男/女神##20:0:颜值担当##21:0:无上述情况##22:3:无上述情况##23:15.432098765432:0';
//        //tag2
//        $finalAnswerStr = '1:1:男##2:0:22-40岁##3:0:165##4:0:55##5:0:无上述情况##6:42:化学污染##7:0:无上述情况##8:12.3:脂肪肝##9:0:无上述情况##10:0:无上述情况##11:9.4:乳腺炎##12:0:无上述情况##13:0:无上述情况##14:0:无上述情况##15:0:无上述情况##16:70:舌淡苔白##17:7:腹泻##18:0:无上述情况##19:7.14:胸肌大些##20:0:无上述情况##21:0:无上述情况##22:3:无上述情况##23:20.20202020202:0';
//        //tag3
//        $finalAnswerStr = '1:0:女##2:50:7-22岁##3:0:180##4:0:50##5:0:无上述情况##6:42:电离辐射##7:0:无上述情况##8:0:无上述情况##9:0:无上述情况##10:0:无上述情况##11:0:无上述情况##12:0:无上述情况##13:0:无上述情况##14:0:无上述情况##15:0:无上述情况##16:70:舌体黯紫有瘀点##17:0:次数规律 色泽正常##18:0:无上述情况##19:0:我就是男/女神##20:0:颜值担当##21:0:无上述情况##22:3:无上述情况##23:15.432098765432:0';
        //有重复标签的情况
//        $finalAnswerStr = '1:0:女##2:50:7-22岁##3:0:160##4:0:80##5:0:无上述情况##6:0:无上述选项##7:0:无上述情况##8:0:无上述情况##9:0:无上述情况##10:0:无上述情况##11:0:无上述情况##12:0:无上述情况##13:0:无上述情况##14:0:无上述情况##15:0:无上述情况##16:0:红润##17:0:次数规律 色泽正常##18:3:易失眠、多梦##19:7.21:瘦瘦瘦##20:5.1:没痘痘##21:5.05:肾虚 你懂的。。。##22:3:无上述情况##23:31.25:0';
        //only level 4
//        $finalAnswerStr = '1:0:女##2:0:22-40岁##3:0:160##4:0:50##5:0:无上述情况##6:0:无上述选项##7:0:无上述情况##8:0:无上述情况##9:0:无上述情况##10:0:无上述情况##11:0:无上述情况##12:0:无上述情况##13:0:无上述情况##14:0:无上述情况##15:0:无上述情况##16:0:无上述情况##17:0:次数规律 色泽正常##18:0:无上述情况##19:0:我就是男/女神##20:0:无上述情况##21:0:无上述情况##22:28.98:大雾霾天的 word肺|||大热天的 快中暑了|||听说最近流感挺多的 宝宝有点怕怕|||天寒地冻 出门冻成狗##23:19.53125:0';
        //only level 4
        $finalAnswerStr = '1:0:女##2:0:22-40岁##3:0:160##4:0:50##5:0:无上述情况##6:0:无上述选项##7:0:无上述情况##8:0:无上述情况##9:0:无上述情况##10:0:无上述情况##11:0:无上述情况##12:0:无上述情况##13:0:无上述情况##14:0:无上述情况##15:0:无上述情况##16:0:无上述情况##17:0:次数规律 色泽正常##18:0:无上述情况##19:0:我就是男/女神##20:5.15:像小志的不老容颜##21:0:无上述情况##22:28.98:大雾霾天的 word肺|||大热天的 快中暑了|||听说最近流感挺多的 宝宝有点怕怕|||天寒地冻 出门冻成狗##23:19.53125:0';

        $score = 207;
        $tid = 24;
        $uid = 9;
        $instance= new RecipeTestingResult($uid,$tid,$finalAnswerStr,$score);
        $result = $instance->result();
        return apiSuccess($result);


//        Redis::select(9);
//        $key = 'USER_LATEST_TAG_';
//        Redis::del($key);
//        return apiSuccess();


    }


    public function initDeveloperTestingRel(){
        $testingList = TestingDetailService::getAll();
        $status = 'FAIL';
        $totalNum = count($testingList);
        if($totalNum > 0){
            $list = [];
            $failTids = [];
            foreach ($testingList as $detail){
                $developer = TestingDeveloperService::getByUnionCode($detail['dev_union_code']);
                if($developer && $developer['union_code'] && $developer['appid']){
                    $data['tid'] = $detail['id'];
                    $data['union_code'] = $detail['dev_union_code'];
                    $data['appid'] = $developer['appid'];
                    array_push($list,$data);
                }else{
                    array_push($failTids,$detail['id']);
                }
            }

            if(count($list) > 0){
                DeveloperTestingRelService::batchInsertData($list);
                $actualImportNum = count($list);
                $status = 'SUCCESS';

            }
        }
        $result['status'] = $status;
        $result['totalNum'] = $totalNum;
        $result['actualImportNum'] = $actualImportNum;
        $result['failTids'] = $failTids;
        return apiSuccess($result);
    }


    /**
     * 批量增加合作伙伴的测评
     */
    public function batchInsertPartnerTesting(Request $request){
        $eid = $request->input('eid');
        $tids =  $request->input('tids');

        if(strlen(trim($eid)) <= 0){
            return apiFailed(null,'eid invalid');
        }
        if(strlen(trim($tids)) <= 0){
            return apiFailed(null,'tids invalid');
        }

        $num = 0;
        $status = 'FAIL';
        if($developer = TestingDeveloperService::getByAbbreviation($eid)){
            $tidArr = explode(',',$tids);
            $list = [];
            foreach ($tidArr as $tid){
                if($detail = TestingDetailService::getById($tid)){
                    $data['tid'] = $detail['id'];
                    $data['union_code'] = $developer['union_code'];
                    $data['appid'] = $developer['appid'];
                    array_push($list,$data);
                }
            }
            if(count($list) > 0){
                $num = count($list);
                DeveloperTestingRelService::batchInsertData($list);
                $status = 'SUCCESS';
            }
        }

        $result = [
            'num' => $num,
            'status' => $status,
        ];
        return apiSuccess($result);
    }


}
